package com.dyz.LeetCodeforSwordFingerOffer;

/**
 * @author: daiyizheng
 * @date: 2021/7/8 23:45
 * @description:
 */
public class MaxProfit63 {


    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0){return 0;}
        int len = prices.length;
        int[] dp = new int[len];
        dp[0] = 0;//第0天的最大理论为0
        int minMon = prices[0];
        for(int i=1;i<len;i++){
            dp[i] = Math.max(dp[i-1], prices[i]-Math.min(minMon, prices[i]));
            minMon = Math.min(minMon, prices[i]);
        }
        return dp[len-1];
    }

    public int maxProfit1(int[] prices) {
        int low=Integer.MAX_VALUE;//当前最低买入价格
        int max=0;//最高利润
        for(int price:prices){
            low=Math.min(low,price);
            max=Math.max(max,price-low);
        }
        return max;
    }
}
